<?php

class DeleteMemberController {
	
	private $m_memberView = "";
	private $m_memberDAL = "";
	private $m_loginView = "";
	private $m_loginHandler = "";
	
	public function __construct ($a_memberView, $a_memberDAL, $a_loginView, $a_loginHandler){
		$this->m_memberView = $a_memberView;
		$this->m_memberDAL = $a_memberDAL;
		$this->m_loginView = $a_loginView;
		$this->m_loginHandler = $a_loginHandler;
	}
	
	public function DoControl (){
		$xhtml = "";

		//xhtml form to terminate the membership 
		if($this->m_loginHandler->IsloggedIn()){
			$xhtml .= $this -> m_memberView -> DoQuitMembershipForm();
		}else{
			return $this-> m_memberView ->YouHaveToLogInMessage();
		}

		 /* 
		 * If user pushes "delete-me-button". The system checks whether or not the user exists and 
		 * that it's a correct password. If not it returns "Not soo good hörredu" in xhtml. 
		 */
		if ($this -> m_memberView -> TriedToDeleteMember()) {
			if($this -> m_memberDAL -> UserNameExists($this->m_loginView -> GetUserName())){
				if ($this -> m_memberDAL -> DeleteMember($this->m_loginView -> GetUserName(), $this->m_loginView-> GetPassword())) {
					if (!$this -> m_memberDAL -> UserNameExists($this->m_loginView -> GetUserName())) {
						$xhtml .= $this -> m_memberView -> DeleteMemberSuccess($this->m_loginView -> GetUserName());
					} else {
						$xhtml .= $this -> m_memberView -> DeleteMemberFail($this->m_loginView -> GetUserName());
					}
				}
			}else{
				$xhtml .= $this ->m_memberView ->MemberNotExist();
				
			}
			
		}
		return $xhtml;
		
	}
}